﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace DnPakExplorer.Forms {
	public partial class DntableUnionSeleteForm : Form {

		public FileHeader[] selectedFileHeaders;

		public DntableUnionSeleteForm(Hashtable excludePaths, DntField[] template) {
			InitializeComponent();
			PakExplorer exlpore = Util.GetExplorer();
			List<FileHeader> headers = new List<FileHeader>();
			exlpore.GetFileHeaders(@"\resource\ext", headers);
			foreach (FileHeader header in headers) {
				if(header.fileName.EndsWith(".dnt", StringComparison.OrdinalIgnoreCase)){
					if (excludePaths[header] == null) {
						using (Stream s = exlpore.GetFileStream(header)) {
							DntField[] fields = Dntable.LoadFields(s);
							bool match = true;
							if (fields.Length == template.Length) {
								for (int i = 0; i < template.Length; i++) {
									if (fields[i].type != template[i].type || fields[i].name != template[i].name) {
										match = false;
										break;
									}
								}
							} else
								match = false;
							if (match) {
								ListViewItem item = listView1.Items.Add(Path.GetFileNameWithoutExtension(header.fileName));
								item.SubItems.Add(header.fileName);
								item.Checked = true;
								item.Tag = header;
							}
						}
					} else {
						
					}
				}
			}
		}

		private void button1_Click(object sender, EventArgs e) {
			List<FileHeader> list = new List<FileHeader>();
			foreach (ListViewItem item in listView1.Items) {
				FileHeader fh = item.Tag as FileHeader;
				if (fh != null && item.Checked) {
					list.Add(fh);
				}
			}
			selectedFileHeaders = list.ToArray();
			DialogResult = System.Windows.Forms.DialogResult.OK;
			Close();
		}

		private void button2_Click(object sender, EventArgs e) {
			DialogResult = System.Windows.Forms.DialogResult.Cancel;
			Close();
		}
	}
}
